/*
 * Template.java
 * v1
 * 28 Marzo 2012
 * 
 * -Este template no se debe modificar 
 * -Para crear un ejecutable se debe copiar este template y quitar los comentarios del main()
 * -Los parametros de Teamname y Ip se insertan en el linea de comandos al momento de ejecutar
 * la nueva clase creada de este template
 * -El codigo del agente se debe insertar en el Case de PLAYON adentro de la funcion run()
 */

public class Template {
	
	public static enum GameState {
		PLAY_ON,
		BEFORE_KICK_OFF,
		TIME_OVER,
		KICK_OFF_SIDE,
		KICK_OFF_OSIDE,
		KICK_IN_SIDE,
		KICK_IN_OSIDE,
		CORNER_KICK_SIDE,
		CORNER_KICK_OSIDE,
		GOAL_KICK_SIDE,
		GOAL_KICK_OSIDE,
		TIME_UP,
		INVALID
	}
	
	public static class Agente implements Runnable{
		private GameState m_state;
		private Player p;
		private boolean initialized = false;
		private boolean isGoalie = true;
		private String side;
		private Formations f;
		private String m_ip;
		private String m_teamName;
		volatile boolean controlFlag;
		
		public Agente(boolean isGoalie, String ip, String teamName) {
			m_ip = ip;
			m_teamName = teamName;
			this.isGoalie = isGoalie;
			this.controlFlag = true;
		}
		
		public void init() {
			//p = new Player("Borregos", "192.168.1.111", false); //192.168.1.100
			p = new Player(m_teamName, m_ip, this.isGoalie);
			System.out.println("Side: " + p.getSide());
			System.out.println("Player number: " + Integer.toString(p.getNumber()));
			System.out.println("Mode:" + p.getServerParam("wind_random"));
			f= new Formations();
			f.getFormation(p.getNumber(), p);
			
			initialized=true;
			m_state = GameState.BEFORE_KICK_OFF;
			this.controlFlag = true;
		}
	
		public 	GameState getGameState(String refereeMessage)
		{
			GameState state;
			if(refereeMessage.equals("play_on")) {
				state = GameState.PLAY_ON;
			} else if (refereeMessage.equals("before_kick_off")) {
				state = GameState.BEFORE_KICK_OFF;
			} else if(refereeMessage.equals("time_over")) {
					state = GameState.TIME_OVER;
			} else if(refereeMessage.equals("kick_off_l") ) {
				if( this.side.equals("l"))
					state = GameState.KICK_OFF_SIDE;
				else
					state = GameState.KICK_OFF_OSIDE;
			} else if(refereeMessage.equals("kick_off_r") ) {
				if( this.side.equals("r")) 
					state = GameState.KICK_OFF_SIDE;
				else 
					state = GameState.KICK_OFF_OSIDE;
			} else if(refereeMessage.equals("free_kick_l")) {
				if( this.side.equals("l"))
					state = GameState.KICK_OFF_SIDE;
				else
					state = GameState.KICK_OFF_OSIDE;
			}
			else if(refereeMessage.equals("free_kick_r")) {
				if( this.side.equals("r"))
					state = GameState.KICK_OFF_SIDE;
				else
					state = GameState.KICK_OFF_OSIDE;
			}
			else if(refereeMessage.equals("corner_kick_l")) {
				if( this.side.equals("l"))
					state = GameState.CORNER_KICK_SIDE;
				else
					state = GameState.CORNER_KICK_OSIDE;
			}
			else if(refereeMessage.equals("corner_kick_r"))	{
				if( this.side.equals("r"))
					state = GameState.CORNER_KICK_SIDE;
				else
					state = GameState.CORNER_KICK_OSIDE;
			}
			else if(refereeMessage.equals("goal_kick_l")) {
				if( this.side.equals("l"))
					state = GameState.GOAL_KICK_SIDE;
				else
					state = GameState.GOAL_KICK_OSIDE;
			}
			else if(refereeMessage.equals("goal_kick_l")) {
				if( this.side.equals("l"))
					state = GameState.GOAL_KICK_SIDE;
				else
					state = GameState.GOAL_KICK_OSIDE;
			}
			else { //No hay estado valido
				state = GameState.INVALID;
			}			
			
			return state;
		}
		
		public void run() {
			while (this.controlFlag) 
			{
				if(!this.initialized) {
					init();
				} else {
					if(p.hasNextRefereeMessage()) {
						String refereeMessage = p.getNextRefereeMessage().text;
						m_state = getGameState( refereeMessage );
					}
				}
			
				switch (m_state) 
				{
					case BEFORE_KICK_OFF:
						break;
					case PLAY_ON: //AQUI SE INSERTA CODIGO PARA EL AGENTE
						p.updateWorldModel(); //Siempre esta
						try { 
							Thread.sleep(50); // El thread se duerme.
						} catch (InterruptedException ex) {
							System.out.println("Error en " + ex.toString());
						}
						break;
					case TIME_UP:
						this.controlFlag = false;
						break;
					default:
						break;
				}
			}
		}	
	}
	
/*
	public static void main(String []args) {
		if(args.length != 3) {
			System.out.println("Invalid Number of Arguments: " + args.length);
			System.out.println("Correct call method is [isGoalie (true/false)] [serverIp] [teamName] ");
			return;
		}
		
		String ip = args[1];
		String teamName = args[2];
	
		Runnable runnable = new Agente(Boolean.parseBoolean(args[0]), ip, teamName);
		Thread th = new Thread(runnable);
		th.start();
	}
*/
	
}
